import re
from shuogg import file_util
import win32clipboard
import win32con


def get_clipboard_text():
    try:
        win32clipboard.OpenClipboard()
        data = win32clipboard.GetClipboardData()
        win32clipboard.CloseClipboard()
        return data
    except Exception as e:
        data = ''
        print('clipboard unknown error!!', e)
    return data


def set_clipboard_text(text):
    """
    设置剪贴板内容
    :param text:
    :return:
    """
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text)
    win32clipboard.CloseClipboard()


def cpp2py(text):
    text = text.replace('true', 'True')
    text = text.replace('NULL', 'None')
    text = text.replace('false', 'False')
    text = text.replace('this ->', 'self.')
    text = text.replace('this->', 'self.')
    text = text.replace('this', 'self')
    text = text.replace('->', '.')
    text = text.replace('void ', 'def ')
    text = re.sub(r'(\s\w+)::', ' ', text)
    text = text.replace('else if', 'elif')
    text = text.replace('else', 'else:')
    text = text.replace('//', '#')
    text = text.replace(';', '')
    text = re.sub('if([^\r\n]+)', r'if\1:', text)
    text = text.replace('{', '')
    text = text.replace('}', '')
    text = re.sub('(def[^\r\n]+)', r'\1:', text)
    text = text.replace('new ', '')
    text = text.replace('*', '')
    text = text.replace('::', '.')
    # 干掉空行
    text = re.sub('\n\s*\r', '\r\n', text)
    text = re.sub('\r\s*\n', '\r\n', text)
    text = text.replace('def', '\r\ndef')
    return text


if __name__ == '__main__':
    text = get_clipboard_text()
    # 直接对剪贴板的内容进行转换
    py_text = cpp2py(text)
    set_clipboard_text(py_text)
    print(py_text)
